package org.springblade.task.job.report.operation;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.operationAnalysis.entity.DailyTaskReport;
import org.springblade.modules.operationAnalysis.service.IDailyTaskReportService;
import org.springblade.task.base.entity.BaseTaskLog;
import org.springblade.task.base.entity.BaseTaskInfo;
import org.springblade.task.base.param.BaseTaskLogParam;
import org.springblade.task.base.service.IBaseTaskLogService;
import org.springblade.task.base.service.IBaseTaskInfoService;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: 每日任务活动数量
 * @author syncwt
 * @version V1.0
 */
@Component
public class DailyTaskReportJob {
	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseTaskLogService BaseTaskLogService;
	@Autowired
	private IBaseTaskInfoService baseTaskInfoService;
	@Autowired
	private IDailyTaskReportService taskReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("taskReportJob")
	public ReturnT<String> taskReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailyTaskReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailyTaskReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		List<BaseTaskInfo> taskInfoList = baseTaskInfoService.list();
		if(!CollectionUtils.isEmpty(taskInfoList)) {
			BaseTaskLogParam param = new BaseTaskLogParam();
			param.setStartDate(DateUtils.getDateStartTime(taskDate));
			param.setEndDate(DateUtils.getDayEndTime(taskDate));
			List<BaseTaskLog> activityLogList = BaseTaskLogService.queryByDateForReport(param);
			Map<String, Integer> integerMap = new HashMap<>();
			if(!CollectionUtils.isEmpty(activityLogList)) {
				for (BaseTaskLog BaseTaskLog: activityLogList) {
					integerMap.put(BaseTaskLog.getTaskName(), BaseTaskLog.getNum());
				}
			}
			List<DailyTaskReport> taskReportList = new ArrayList<>();
			DailyTaskReport taskReport = null;
			for (BaseTaskInfo taskInfo: taskInfoList) {
				taskReport = new DailyTaskReport();
				taskReport.setTaskName(taskInfo.getTaskName());
				taskReport.setDate(taskDate);
				taskReport.setTaskUserNum(integerMap.get(taskInfo.getTaskName()) == null ? 0 : integerMap.get(taskInfo.getTaskName()));
				taskReportList.add(taskReport);
			}

			taskReportService.saveBatchTaskReport(taskReportList);
		}
	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}


}
